<?php
require_once 'config.php';

session_start();

// 验证部门用户权限
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'department') {
    header("Location: index.html");
    exit();
}

$department = $_SESSION['department'];
$conn = getDBConnection();

// 获取历史记录
$history = $conn->query("SELECT name, old_status, new_status, note, changed_at, is_deleted 
                         FROM status_history 
                         WHERE name IN (SELECT name FROM personnel WHERE department = '$department')
                         OR is_deleted = TRUE
                         ORDER BY changed_at DESC");

$conn->close();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $department; ?>历史记录</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="history-container">
        <h1><?php echo $department; ?>历史记录</h1>
        
        <div class="history-list">
            <table>
                <tr>
                    <th>姓名</th>
                    <th>原状态</th>
                    <th>新状态</th>
                    <th>备注</th>
                    <th>变更时间</th>
                </tr>
                <?php while ($row = $history->fetch_assoc()): ?>
                <tr>
                    <td><?php echo $row['is_deleted'] ? $row['name'].' (已删除)' : $row['name']; ?></td>
                    <td><?php echo $row['old_status'] ?? '-'; ?></td>
                    <td><?php echo $row['new_status']; ?></td>
                    <td><?php echo $row['note'] ?? '-'; ?></td>
                    <td><?php echo $row['changed_at']; ?></td>
                </tr>
                <?php endwhile; ?>
            </table>
        </div>
        
        <div class="navigation">
            <a href="department.php" class="btn">人员管理</a>
            <a href="status.php" class="btn">状态管理</a>
            <a href="logout.php" class="logout-btn">退出登录</a>
        </div>
    </div>
</body>
</html>